<?php
/**
 * 支付工厂类
 * User: gyc
 * Update: wika/zym
 * Date: 2016/6/13
 * Time: 15:09
 */
require_once('Alipay.php');
require_once('WeiXinPay.php');
class PayFactoryService
{
    public function createObj($payType,$accountId,$orderSn,$title,$notifyUrl='',$returnUrl='',$ledger = array()){
        switch ($payType){
            case 'weixin':
                return new WeiXinPay($accountId,$orderSn,$title,$notifyUrl,$returnUrl,$ledger);
                break;
            case 'alipay':
                return new Alipay($accountId,$orderSn,$title,$notifyUrl,$returnUrl);
                break;
        }
    }

    /**
     * 一卡通消费(方法封装)
     * @param $oneCard 一卡通商户信息
     * @param $oneCardCard 一卡通用户信息
     * @param $account_id 平台账号
     * @param $user_id 用户id
     * @param $order_amount 消费金额
     * @param $trade_type 消费类型 1:校园订餐 2:在线订餐 3:班车购票
     * @param $order_sn 订单编号
     * @return array  status = S 成功|F 失败; 成功返回data：平台一卡通消费订单号order_id
     */
	public function OneCardPay($oneCard,$oneCardCard,$account_id,$user_id,$order_amount,$trade_type,$order_sn)
    {
        if (!$oneCard || !$oneCardCard || !$account_id || !$user_id || !$order_amount || !$trade_type) {
            return array('status' => 'F', 'message' => '参数错误');
        }
        //消费接口
        vendor("OneCard.OneCardApi");
        $OneCardApi = new OneCardApi($oneCard['md5_key'], $oneCard['interface_url']);
        $result = $OneCardApi->PaymentA($order_amount, $oneCard, $oneCardCard);
        if (!isset($result['status'])) {
            wLog(date('Y-m-d H:i:s') . ' OneCardPay PaymentA exception: ' . var_export($result, true), 'OneCardPay');
            return array('status' => 'F', 'message' => '系统异常');
        }
        if ($result['status'] != 'S') {
            wLog(date('Y-m-d H:i:s') . ' OneCardPay PaymentA faild: ' . var_export($result, true), 'OneCardPay');
            return array('status' => 'F', 'message' => $result['message']);
        }

        //扣款成功
        $browser = 0; //通过浏览器调取不用的用户信息和支付信息
        if (strpos($_SERVER['HTTP_USER_AGENT'], 'MicroMessenger') != false) {
            $browser = 1;
        } elseif (strpos($_SERVER['HTTP_USER_AGENT'], 'Alipay') != false) {
            $browser = 2;
        }

        //获取余额
        $resultBalance = $OneCardApi->getAccDBMoney($oneCard, $oneCardCard);
        if ($resultBalance["status"] == "S") {
            $balance = $resultBalance['money'] ? round($resultBalance['money'] / 100, 2) : 0;
            $balance = $balance - $order_amount;
        } else {
            $balance = $oneCardCard['balance'] - $order_amount;
            wLog(date('Y-m-d H:i:s') . ' OneCardPay getAccDBMoney faild: ' . var_export($resultBalance, true), 'OneCardPay');
        }

        //创建一卡通消费订单
        $order = array(
            'account_id' => $account_id,
            'one_card_id' => $oneCardCard['one_card_id'],
            'card_id' => $oneCardCard['card_id'],
            'order_sn' => $order_sn,
            'user_id' => $user_id,
            'number' => $oneCardCard['number'],
            'user_name' => $oneCardCard['user_name'],
            'pay_type' => $browser,
            'order_type' => 2, //订单类型:消费
            'trade_type' => $trade_type,
            'alipay_pay_id' => session("alipayId"),
            'alipay_user_id' => session("alipayUserId"),
            'wechat_pay_id' => session("wechatId"),
            'wechat_user_id' => session("wechatUserId"),
            'create_time' => time(),
            'pay_time' => time(),
            'finish_time' => time(),
            'status' => 1, //订单状态：扣款成功
            'order_amount' => $order_amount,
            'goods_amount' => $order_amount,
            'balance' => $balance,
            'trade_flow' => $result['data']
        );
        $res = M('OneCardRecharge')->add($order);
        if ($res) {
            //更新用户账户一卡通余额
            M("OneCardCard")->where(array("card_id" => $oneCardCard['card_id']))->save(array("balance" => $balance));
            return array('status' => 'S', 'data' => $res);
        } else {
            wLog(date('Y-m-d H:i:s') . ' OneCardPay OneCardRecharge add faild: ' . M()->_sql(), 'OneCardPay');
            return array('status' => 'S', 'data' => $res);
        }
    }
}